import uuid

import snowflake.client
from flask import Blueprint,request,jsonify
import json
from util.myredis import r

from models.user import *

order_blue = Blueprint("order",__name__)


@order_blue.route("/getorder")
def getorder():
    sql = goods.query.all()
    list = []
    for i in sql:
        list.append({
            'id': i.id,
            'url': i.url,
            'money': i.money,
            'descs': i.descs,
            'userid': i.userid
        })
    return jsonify({'code': 200, 'data': list})


@order_blue.route("/addgoods", methods=['POST'])
def getorderid():
    data = json.loads(request.data)
    userid = int(data['userid'])
    goodsid = int(data['goodsid'])
    pkey = 'goods' + str(userid)
    r.insert_hash(pkey, goodsid, userid)
    return jsonify({'code': 200, 'msg': '加入购物车成功'})


@order_blue.route("/getgoods")
def getgoods():
    userid = request.args.get('userid')
    pkey = 'goods' + str(userid)
    res = r.get_all_hash(pkey)
    print(res)
    list = []
    for i,j in res.items():
        print(i,'___________')
        goodsid = i.decode()
        key = 'sales_volume' + str(goodsid)
        c = r.str_get(key)
        print(c)
        if c:
            sql = db.session.execute("select * from goods where id=%d" % int(goodsid))
            for x in sql:
                list.append({
                    'id': x.id,
                    'url': x.url,
                    'money': x.money,
                    'descs': x.descs,
                    'sales': int(c.decode())
                })
        else:
            sql = db.session.execute("select * from goods where id=%d" % int(goodsid))
            for x in sql:
                list.append({
                    'id': x.id,
                    'url': x.url,
                    'money': x.money,
                    'descs': x.descs,
                    'sales': 0
                })
    return jsonify({'code': 200, 'data': list})


@order_blue.route("/postorder", methods=['POST'])
def postorder():
    data = json.loads(request.data)
    number = data['id']
    order_token = data['token']
    userid = data['userid']
    value = r.str_get(order_token)
    if not value:
        return jsonify({'code': 200, 'msg': '已经生成不能重复加入'})
    r.del_key(order_token)
    ord = order(number=number, userid=userid, status=1)
    pkey = 'goods' + str(userid)
    key = 'sales_volume' + str(number)
    c = r.str_get(key)
    if c:
        a = int(c) + 1
        r.str_add(key, a)
    else:
        r.str_add(key, 1)
    r.del_hash(pkey, number)
    db.session.add(ord)
    db.session.commit()
    # 获取到当前商品的价格
    good = goods.query.filter_by(id=number).first()
    userids = good.userid
    money = good.money

    # 获取到发布服务的人
    user = User.query.filter_by(id=userids).first()
    tmoney = user.money
    umoney = money+tmoney
    print(tmoney, umoney,'ssssss')
    User.query.filter_by(id=userids).update({'money': umoney})
    print(1)
    db.session.commit()
    print(2)

    # 购买人的id
    users = User.query.filter_by(id=userid).first()
    gmoney = users.money
    smoney = gmoney-money
    print(gmoney, smoney,'ssssss')

    User.query.filter_by(id=userid).update({'money': smoney})
    print(3)
    db.session.commit()
    print(4)
    return jsonify({'code': 200, 'msg': '购买成功'})


# 浏览记录
@order_blue.route("/browsing")
def browsing():
    userid = request.args.get('userid')
    url = request.args.get('url')
    pkey = str(userid)
    key = 'http://localhost:8080' + url
    r.insert_set(pkey, key)
    return jsonify({'code': 200, 'msg': 'ok'})

# 查看浏览记录
@order_blue.route("/getbrowsing")
def getbrowsing():
    userid = request.args.get('userid')
    pkey = str(userid)
    res = r.find_set(pkey)
    print(res)
    return jsonify({'code': 200, 'data': res})


@order_blue.route("/m_token")
def m_token():
    # 生成token
    token = uuid.uuid1().hex
    print(token)
    # 将token保存redis
    r.str_add(token, '1')
    # 返回结果
    return jsonify({
        'code': 200,
        'msg': 'ok',
        'token': token
    })


























